<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:php="http://php.net/xsl" exclude-result-prefixes="php">

    <xsl:include href="utility.xsl" />
    <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" />
    
    <xsl:variable name="TIMESTAMP" select="number(php:functionString('time'))" />

    <xsl:template match="/response">
    <xsl:element name="div">
    <xsl:attribute name="id">melative-library</xsl:attribute>
    <xsl:attribute name="class">library-0</xsl:attribute>
    <div id="controls" style="display:none;">
        <ul>
            <li class="break">Mark</li>
            <li>Completed</li>
            <li>Current</li>
            <li>Backlog</li>
            <li>Dropped</li>
            <li class="break">Library</li>
            <!--<li>Edit</li>-->
            <li>Remove</li>
        </ul>
    </div>
    <div class="tabs">
    <xsl:call-template name="library-tabs">
        <xsl:with-param name="states" select="string('current,Current|subscribed,Subscribed|complete,Completed|wishlisted,Wishlisted|planned,Planned|marathon,Marathoning|hold,On-Hold|backlog,Backlogged|dropped,Dropped|started,Started|paused,Paused|resumed,Resumed|unknown,Other')" />
        <xsl:with-param name="active" select="0" />
    </xsl:call-template>
    </div>
    
    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('current')" />
        <xsl:with-param name="title" select="string('Current')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('subscribed')" />
        <xsl:with-param name="title" select="string('Subscribed')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('complete')" />
        <xsl:with-param name="title" select="string('Completed')" />
    </xsl:call-template>
    
    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('wishlisted')" />
        <xsl:with-param name="title" select="string('Wishlisted')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('planned')" />
        <xsl:with-param name="title" select="string('Planned')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('marathon')" />
        <xsl:with-param name="title" select="string('Marathon')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('hold')" />
        <xsl:with-param name="title" select="string('On-Hold')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('backlog')" />
        <xsl:with-param name="title" select="string('Backlog')" />
    </xsl:call-template>

    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('dropped')" />
        <xsl:with-param name="title" select="string('Dropped')" />
    </xsl:call-template>
    
    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('started')" />
        <xsl:with-param name="title" select="string('Started')" />
    </xsl:call-template>
    
    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('paused')" />
        <xsl:with-param name="title" select="string('Paused')" />
    </xsl:call-template>
    
    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('resume')" />
        <xsl:with-param name="title" select="string('Resumed')" />
    </xsl:call-template>
    
    <xsl:call-template name="library-block">
        <xsl:with-param name="state" select="string('unknown')" />
        <xsl:with-param name="title" select="string('Unknown')" />
    </xsl:call-template>
        </xsl:element>
    </xsl:template>
    
    <xsl:template name="library-tabs">
        <xsl:param name="states" />
        <xsl:param name="active" />
        <xsl:variable name="state" select="substring-before(substring-before($states,'|'),',')" />
        <xsl:variable name="display" select="substring-after(substring-before($states,'|'),',')" />
        <xsl:variable name="count" select="count(library/record[$state = state])" />
        
        <xsl:if test="$states != ''">
            <xsl:if test="$count &gt; 0">
                <xsl:element name="a">
                    <xsl:attribute name="class">tab <xsl:if test="$active = 0"><xsl:text>active </xsl:text></xsl:if></xsl:attribute>
                    <xsl:attribute name="href">#state-<xsl:value-of select="$state" /></xsl:attribute>
                    <xsl:value-of select="$display" /><xsl:text> </xsl:text><small>(<xsl:value-of select="$count" />)</small>
                </xsl:element>
            </xsl:if>
            <xsl:call-template name="library-tabs">
                <xsl:with-param name="states" select="substring-after($states,'|')" />
                <xsl:with-param name="active" select="$active - 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="library-block">
        <xsl:param name="state" />
        <xsl:param name="title" />
        
        <xsl:if test="count(library/record[$state = state]) &gt; 0">
            <xsl:element name="h3">
                <xsl:attribute name="id">state-<xsl:value-of select="$state" /></xsl:attribute>
                <xsl:value-of select="$title" /></xsl:element>
            <xsl:element name="ul">
            <xsl:attribute name="class">liblock <xsl:if test="not(contains('anime|adrama|literature|lightnovel|manga', //entity/type))"><xsl:text> no-progress </xsl:text></xsl:if> <xsl:value-of select="$state" /></xsl:attribute>
                <li class="block-header">
                <ul>
                    <li class="sort-title"><img src="/wp-content/plugins/melative-item/style/pixel.gif" alt="sort" /> Title</li>
                    <li class="sort-rating"><img src="/wp-content/plugins/melative-item/style/pixel.gif" alt="sort" /> Rating</li>
                    <li class="sort-progress"><img src="/wp-content/plugins/melative-item/style/pixel.gif" alt="sort" /> Progress</li>
                    <li class="sort-updated"><img src="/wp-content/plugins/melative-item/style/pixel.gif" alt="sort" /> Updated</li>
                    <li class="clear"><xsl:text> </xsl:text></li>
                </ul>
                </li>
            <xsl:apply-templates select="library/record[contains($state, state)]">
                <xsl:sort select="entity/aliase" order="ascending" />
            </xsl:apply-templates>
            </xsl:element>
        </xsl:if>
    </xsl:template>
    
    <!--
    <record>
      <added>2010-04-07 20:16:27</added>
      <updated>2010-04-07 20:54:32</updated>
      <state>current</state>
      <completions>0</completions>
      <rating></rating>
      <status>Nya~! Right, so, I love it. For the energy.</status>
      <tags></tags>
      <favorite></favorite>
      <segment>
        <type>Episode</type>
        <name>1</name>
      </segment>
      <entity>
        <id>34063</id>
        <type>anime</type>
        <image_url>http://images.melative.com/media_6ixecjyfuesi7kdki13jvxlxht39pc.200.jpg</image_url>
        <aliase>Mayoi Neko Overrun!</aliase>
        <aliase>Stray Cats Overrun!</aliase>
        <aliase>迷い猫オーバーラン!</aliase>
        <meta>
          <first>2010-04</first>
          <season>spring</season>
          <type>series</type>
          <id>anidb:7229</id>
          <id>ann:11175</id>
          <id>mal:7590</id>
          <genre>comedy</genre>
          <length>12</length>
          <duration>24m</duration>
          <statu>Airing</statu>
        </meta>
        <length>12</length>
        <duration>1440</duration>
      </entity>
      <system>
        <users>4</users>
        <approval></approval>
        <rank>483</rank>
      </system>
    </record>
    -->
    
    <xsl:template match="record">
        <xsl:element name="li">
            <xsl:attribute name="id"><xsl:value-of select="concat(entity/type,'-',entity/id)" /></xsl:attribute>
            <xsl:attribute name="class">listing<xsl:if test="favorite = '1'"><xsl:text> is-fav</xsl:text></xsl:if></xsl:attribute>
            <ul>
            <xsl:element name="li">
                <xsl:attribute name="class">title</xsl:attribute>
            <img src="/wp-content/plugins/melative-item/style/pixel.gif" class="fav" alt="fav" title="Heart Favorite" /><xsl:text> </xsl:text><xsl:element name="a"><xsl:attribute name="href">http://melative.com/<xsl:value-of select="concat(entity/type,'/',entity/aliase)" /></xsl:attribute><xsl:attribute name="rel">melative</xsl:attribute><xsl:value-of select="entity/aliase" /></xsl:element><!--<xsl:call-template name="season-year" />-->
                <xsl:if test="contains('music|album', entity/type) and segment/name != ''"><br />
                    <small><strong>Last: </strong> <xsl:value-of select="segment/name" /></small>
                <!-- display last track -->
                </xsl:if>
                <!--<br />
                <small><xsl:value-of select="state" /></small>-->
            </xsl:element>
            <li class="rating"><xsl:if test="rating != ''"><xsl:value-of select="format-number(rating div 100,'#.0#')" /></xsl:if></li>
            <xsl:element name="li">
                <xsl:attribute name="class"><xsl:text>progress </xsl:text>percent-<xsl:call-template name="progress-percent" /></xsl:attribute>
                <xsl:choose>
                    <xsl:when test="contains('anime|adrama|tv|manga|literature|lightnovel',entity/type)">
                        <xsl:choose>
                            <xsl:when test="segment/name != '' and entity/meta/length != ''"><xsl:value-of select="segment/name" />/<xsl:value-of select="entity/meta/length" />
                            </xsl:when>
                            <xsl:when test="segment/name != ''"><xsl:value-of select="segment/name" />/??</xsl:when>
                            <xsl:when test="entity/meta/length != ''">0/<xsl:value-of select="entity/meta/length" /></xsl:when>
                            <xsl:otherwise></xsl:otherwise>
                        </xsl:choose>
                    </xsl:when>
                    <xsl:otherwise><xsl:value-of select="state" /></xsl:otherwise>
                </xsl:choose>
            </xsl:element>
            <li class="adjust">
                <a class="inc"> </a>
                <a class="dec"> </a>
            </li>
            <li class="updated">
                <xsl:element name="span">
                    <xsl:attribute name="class">utc-<xsl:value-of select="utc_updated" /></xsl:attribute>
                    <xsl:attribute name="title"><xsl:value-of select="substring(updated,1,10)" /></xsl:attribute>
                    <xsl:call-template name="relative-time">
                        <xsl:with-param name="diff" select="$TIMESTAMP - utc_updated" />
                    </xsl:call-template>
                </xsl:element>
            </li>
            <li class="control"><xsl:text> </xsl:text></li>
            <li class="clear"><xsl:text> </xsl:text></li>
            </ul>
        </xsl:element>
        <!--<xsl:call-template name="details" />-->
    </xsl:template>
    
    <xsl:template name="season-year">
    <xsl:if test="entity/meta/season or entity/meta/first">
    <xsl:text> </xsl:text><small>(<xsl:if test="entity/meta/season"><xsl:value-of select="entity/meta/season" /><xsl:text> </xsl:text></xsl:if><xsl:value-of select="substring(entity/meta/first,1,4)" />)</small>
    </xsl:if>
    </xsl:template>
    
    <xsl:template name="details">
        <xsl:element name="tr">
        <xsl:attribute name="class">details <xsl:value-of select="concat(entity/type,'-',entity/id)" /></xsl:attribute>
        <td colspan="4">
        <span class=""></span>
        <xsl:if test="entity/image_url != ''">
            
            <xsl:element name="img">
                <xsl:attribute name="src"> </xsl:attribute>
                <xsl:attribute name="alt"><xsl:value-of select="entity/image_url" /></xsl:attribute>
            </xsl:element>
        
        </xsl:if>
            
            <div class="record">
                <h5>Progress</h5>
                <p class="progress">
                    <dl>
                    <dt>In Library:</dt> <dd><xsl:value-of select="state" /></dd>
                    <dt>Updated:</dt> <dd><xsl:value-of select="updated" /></dd>
                    </dl>
                    <xsl:call-template name="progress-bar" />
                </p>
                
                <h5>Status</h5>
                <p class="status"><xsl:if test="status = ''">[Add Status]</xsl:if><xsl:value-of select="status" /></p>
                
                <h5>Tags</h5>
                <p class="tags"><xsl:if test="tags = ''">[Add Tags]</xsl:if>
                    <xsl:call-template name="tags">
                        <xsl:with-param name="tags" select="concat(tags, ', ')" />
                    </xsl:call-template>
                </p>
                
                <h5>Information</h5>
                <p class="dates">
                    <dl>
                    <dt>Added:</dt> <dd><xsl:value-of select="added" /></dd>
                    <dt>Completions:</dt> <dd><xsl:value-of select="completions" /></dd>
                    <dt>Rating:</dt> <dd><xsl:if test="rating = ''">N/A</xsl:if><xsl:value-of select="rating" /></dd>
                    <dt>Favorite:</dt> <dd><xsl:if test="favorite=1">Yes</xsl:if><xsl:if test="favorite=''">No</xsl:if></dd>
                    </dl>
                </p>
                
                <h5>On Melative</h5>
                <p class="sys">
                    <dl>
                    <dt>Users:</dt> <dd><xsl:if test="system/users = ''">N/A</xsl:if><xsl:value-of select="system/users" /></dd>
                    <dt>Approval:</dt> <dd><xsl:if test="system/approval = ''">N/A</xsl:if><xsl:value-of select="system/approval" /></dd>
                    <dt>Rank:</dt> <dd><xsl:if test="system/rank = ''">N/A</xsl:if><xsl:value-of select="system/rank" /></dd>
                    </dl>
                </p>
                
            </div>
            
            <div class="control">
            <button>Update</button>
            <button style="display:none;">Revert</button>
            </div>
        </td></xsl:element>
    </xsl:template>
    
    <xsl:template name="tags">
        <xsl:param name="tags" />
        <xsl:variable name="next" select="substring-after($tags,', ')" />
        
        <xsl:if test="$tags != ''">
            <a><xsl:value-of select="substring-before($tags,',')" /></a>
            <xsl:if test="$next != ''"><xsl:text>, </xsl:text></xsl:if>
            <xsl:call-template name="tags">
                <xsl:with-param name="tags" select="$next" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="progress-bar">
        <xsl:variable name="progress"><xsl:call-template name="progress-percent" /></xsl:variable>
        <xsl:if test="$progress != 0 and $progress &lt; 100">
            <xsl:element name="span">
                <xsl:attribute name="class">bar</xsl:attribute>
                <xsl:attribute name="style">width:<xsl:value-of select="$progress" />%;</xsl:attribute>
                <xsl:value-of select="$progress" />%
            </xsl:element>
        </xsl:if>
    </xsl:template>
    
    <xsl:template name="progress-percent">
        <xsl:variable name="cur" select="number(segment/name)" />
        <xsl:variable name="max" select="number(entity/meta/length)" />
        <xsl:choose><xsl:when test="$cur &gt; 0 and $max &gt; 0"><xsl:value-of select="round($cur div $max * 100)" /></xsl:when>
        <xsl:otherwise>0</xsl:otherwise></xsl:choose>
    </xsl:template>
    
</xsl:stylesheet>
